package com.example.administrator.zkdemo.fragment;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.administrator.zkdemo.activity.ZKSignInActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zkteco.android.model.ZKADVideoBean;
import com.zkteco.android.utils.ZKFilePath;
import com.zkteco.android.utils.ZKFileUtils;
import com.zkteco.android.utils.ZKSPUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;

public class ZKVideoADFragment extends ZKBaseFragment {
    public static final String TAG = ZKVideoADFragment.class.getCanonicalName();

    @BindView(2131296283)
    SurfaceView adVideo;
    private MediaPlayer mPlayer;
    private List<String> pathData = new ArrayList();

    @BindView(2131296500)
    RelativeLayout rlMain;
    private SurfaceHolder surfaceHolder;
    private int videoNo;

    private void initData() {
        Object localObject2 = new ArrayList();
        String str = (String) ZKSPUtils.getParam(getActivity(), "AD_VIDEO", "");
        Object localObject1 = localObject2;
        if (str != null) {
            localObject1 = localObject2;
            if (!str.equals(""))
                localObject1 = (List) new Gson().fromJson(str, new TypeToken() {
                }
                        .getType());
        }
        localObject2 = new File(ZKFilePath.AD_VIDEO_PATH).listFiles();
        int j = localObject2.length;
        int i = 0;
        while (i < j) {
            str = localObject2[i];
            if ((!str.isDirectory()) && (ZKFileUtils.checkVideoSuffix(str.getName())) && (((List) localObject1).size() > 0)) {
                Iterator localIterator = ((List) localObject1).iterator();
                while (localIterator.hasNext()) {
                    if (!((ZKADVideoBean) localIterator.next()).getName().equals(str.getName()))
                        continue;
                    this.pathData.add(str.getAbsolutePath());
                }
            }
            i += 1;
        }
    }

    private void initListener() {
        this.adVideo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                ((ZKSignInActivity) ZKVideoADFragment.this.getActivity()).closeAD(ZKVideoADFragment.this);
            }
        });
    }

    private void initPlayer() {
        this.mPlayer = new MediaPlayer();
        this.mPlayer.setAudioStreamType(3);
        this.mPlayer.setDisplay(this.surfaceHolder);
        this.mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer paramMediaPlayer) {
                ZKVideoADFragment.this.onVideoCompleted(paramMediaPlayer);
            }
        });
        try {
            this.mPlayer.setDataSource((String) this.pathData.get(this.videoNo));
            this.mPlayer.prepare();
            this.mPlayer.start();
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
        this.mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer paramMediaPlayer) {
                paramMediaPlayer.start();
            }
        });
    }

    private void initView() {
        this.surfaceHolder = this.adVideo.getHolder();
        this.surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1, int paramInt2, int paramInt3) {
                ZKVideoADFragment.this.initPlayer();
            }

            public void surfaceCreated(SurfaceHolder paramSurfaceHolder) {
            }

            public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder) {
            }
        });
    }

    private void onVideoCompleted(MediaPlayer paramMediaPlayer) {
        paramMediaPlayer.setDisplay(null);
        paramMediaPlayer.release();
        this.mPlayer = new MediaPlayer();
        this.mPlayer.setAudioStreamType(3);
        this.mPlayer.setDisplay(this.surfaceHolder);
        this.mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer paramMediaPlayer) {
                ZKVideoADFragment.this.onVideoCompleted(paramMediaPlayer);
            }
        });
        try {
            this.videoNo += 1;
            if (this.pathData.size() <= this.videoNo)
                this.videoNo = 0;
            this.mPlayer.setDataSource((String) this.pathData.get(this.videoNo));
            this.mPlayer.prepare();
            this.mPlayer.start();
            return;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_video_ad;
    }

    public void onActivityCreated(@Nullable Bundle paramBundle) {
        super.onActivityCreated(paramBundle);
        initData();
        initView();
        initListener();
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.mPlayer != null) {
            if (this.mPlayer.isPlaying())
                this.mPlayer.stop();
            this.mPlayer.release();
        }
        this.mPlayer = null;
    }

    public void onStop() {
        super.onStop();
        if ((this.mPlayer != null) && (this.mPlayer.isPlaying()))
            this.mPlayer.pause();
    }
}
